{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Regressão Linear Multivariada - Trabalho\n",
    "\n",
    "## Estudo de caso: Qualidade de Vinhos\n",
    "\n",
    "Nesta trabalho, treinaremos um modelo de regressão linear usando descendência de gradiente estocástico no conjunto de dados da Qualidade do Vinho. O exemplo pressupõe que uma cópia CSV do conjunto de dados está no diretório de trabalho atual com o nome do arquivo *winequality-white.csv*.\n",
    "\n",
    "O conjunto de dados de qualidade do vinho envolve a previsão da qualidade dos vinhos brancos em uma escala, com medidas químicas de cada vinho. É um problema de classificação multiclasse, mas também pode ser enquadrado como um problema de regressão. O número de observações para cada classe não é equilibrado. Existem 4.898 observações com 11 variáveis de entrada e 1 variável de saída. Os nomes das variáveis são os seguintes:\n",
    "\n",
    "1. Fixed acidity.\n",
    "2. Volatile acidity.\n",
    "3. Citric acid.\n",
    "4. Residual sugar.\n",
    "5. Chlorides.\n",
    "6. Free sulfur dioxide. \n",
    "7. Total sulfur dioxide. \n",
    "8. Density.\n",
    "9. pH.\n",
    "10. Sulphates.\n",
    "11. Alcohol.\n",
    "12. Quality (score between 0 and 10).\n",
    "\n",
    "O desempenho de referencia de predição do valor médio é um RMSE de aproximadamente 0.148 pontos de qualidade.\n",
    "\n",
    "Utilize o exemplo apresentado no tutorial e altere-o de forma a carregar os dados e analisar a acurácia de sua solução. \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import sklearn as skt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = pd.read_csv('winequality-white.csv', delimiter=\";\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<bound method NDFrame.head of       fixed acidity  volatile acidity  citric acid  residual sugar  chlorides  \\\n",
       "0               7.0             0.270         0.36           20.70      0.045   \n",
       "1               6.3             0.300         0.34            1.60      0.049   \n",
       "2               8.1             0.280         0.40            6.90      0.050   \n",
       "3               7.2             0.230         0.32            8.50      0.058   \n",
       "4               7.2             0.230         0.32            8.50      0.058   \n",
       "5               8.1             0.280         0.40            6.90      0.050   \n",
       "6               6.2             0.320         0.16            7.00      0.045   \n",
       "7               7.0             0.270         0.36           20.70      0.045   \n",
       "8               6.3             0.300         0.34            1.60      0.049   \n",
       "9               8.1             0.220         0.43            1.50      0.044   \n",
       "10              8.1             0.270         0.41            1.45      0.033   \n",
       "11              8.6             0.230         0.40            4.20      0.035   \n",
       "12              7.9             0.180         0.37            1.20      0.040   \n",
       "13              6.6             0.160         0.40            1.50      0.044   \n",
       "14              8.3             0.420         0.62           19.25      0.040   \n",
       "15              6.6             0.170         0.38            1.50      0.032   \n",
       "16              6.3             0.480         0.04            1.10      0.046   \n",
       "17              6.2             0.660         0.48            1.20      0.029   \n",
       "18              7.4             0.340         0.42            1.10      0.033   \n",
       "19              6.5             0.310         0.14            7.50      0.044   \n",
       "20              6.2             0.660         0.48            1.20      0.029   \n",
       "21              6.4             0.310         0.38            2.90      0.038   \n",
       "22              6.8             0.260         0.42            1.70      0.049   \n",
       "23              7.6             0.670         0.14            1.50      0.074   \n",
       "24              6.6             0.270         0.41            1.30      0.052   \n",
       "25              7.0             0.250         0.32            9.00      0.046   \n",
       "26              6.9             0.240         0.35            1.00      0.052   \n",
       "27              7.0             0.280         0.39            8.70      0.051   \n",
       "28              7.4             0.270         0.48            1.10      0.047   \n",
       "29              7.2             0.320         0.36            2.00      0.033   \n",
       "...             ...               ...          ...             ...        ...   \n",
       "4868            5.8             0.230         0.31            4.50      0.046   \n",
       "4869            6.6             0.240         0.33           10.10      0.032   \n",
       "4870            6.1             0.320         0.28            6.60      0.021   \n",
       "4871            5.0             0.200         0.40            1.90      0.015   \n",
       "4872            6.0             0.420         0.41           12.40      0.032   \n",
       "4873            5.7             0.210         0.32            1.60      0.030   \n",
       "4874            5.6             0.200         0.36            2.50      0.048   \n",
       "4875            7.4             0.220         0.26            1.20      0.035   \n",
       "4876            6.2             0.380         0.42            2.50      0.038   \n",
       "4877            5.9             0.540         0.00            0.80      0.032   \n",
       "4878            6.2             0.530         0.02            0.90      0.035   \n",
       "4879            6.6             0.340         0.40            8.10      0.046   \n",
       "4880            6.6             0.340         0.40            8.10      0.046   \n",
       "4881            5.0             0.235         0.27           11.75      0.030   \n",
       "4882            5.5             0.320         0.13            1.30      0.037   \n",
       "4883            4.9             0.470         0.17            1.90      0.035   \n",
       "4884            6.5             0.330         0.38            8.30      0.048   \n",
       "4885            6.6             0.340         0.40            8.10      0.046   \n",
       "4886            6.2             0.210         0.28            5.70      0.028   \n",
       "4887            6.2             0.410         0.22            1.90      0.023   \n",
       "4888            6.8             0.220         0.36            1.20      0.052   \n",
       "4889            4.9             0.235         0.27           11.75      0.030   \n",
       "4890            6.1             0.340         0.29            2.20      0.036   \n",
       "4891            5.7             0.210         0.32            0.90      0.038   \n",
       "4892            6.5             0.230         0.38            1.30      0.032   \n",
       "4893            6.2             0.210         0.29            1.60      0.039   \n",
       "4894            6.6             0.320         0.36            8.00      0.047   \n",
       "4895            6.5             0.240         0.19            1.20      0.041   \n",
       "4896            5.5             0.290         0.30            1.10      0.022   \n",
       "4897            6.0             0.210         0.38            0.80      0.020   \n",
       "\n",
       "      free sulfur dioxide  total sulfur dioxide  density    pH  sulphates  \\\n",
       "0                    45.0                 170.0  1.00100  3.00       0.45   \n",
       "1                    14.0                 132.0  0.99400  3.30       0.49   \n",
       "2                    30.0                  97.0  0.99510  3.26       0.44   \n",
       "3                    47.0                 186.0  0.99560  3.19       0.40   \n",
       "4                    47.0                 186.0  0.99560  3.19       0.40   \n",
       "5                    30.0                  97.0  0.99510  3.26       0.44   \n",
       "6                    30.0                 136.0  0.99490  3.18       0.47   \n",
       "7                    45.0                 170.0  1.00100  3.00       0.45   \n",
       "8                    14.0                 132.0  0.99400  3.30       0.49   \n",
       "9                    28.0                 129.0  0.99380  3.22       0.45   \n",
       "10                   11.0                  63.0  0.99080  2.99       0.56   \n",
       "11                   17.0                 109.0  0.99470  3.14       0.53   \n",
       "12                   16.0                  75.0  0.99200  3.18       0.63   \n",
       "13                   48.0                 143.0  0.99120  3.54       0.52   \n",
       "14                   41.0                 172.0  1.00020  2.98       0.67   \n",
       "15                   28.0                 112.0  0.99140  3.25       0.55   \n",
       "16                   30.0                  99.0  0.99280  3.24       0.36   \n",
       "17                   29.0                  75.0  0.98920  3.33       0.39   \n",
       "18                   17.0                 171.0  0.99170  3.12       0.53   \n",
       "19                   34.0                 133.0  0.99550  3.22       0.50   \n",
       "20                   29.0                  75.0  0.98920  3.33       0.39   \n",
       "21                   19.0                 102.0  0.99120  3.17       0.35   \n",
       "22                   41.0                 122.0  0.99300  3.47       0.48   \n",
       "23                   25.0                 168.0  0.99370  3.05       0.51   \n",
       "24                   16.0                 142.0  0.99510  3.42       0.47   \n",
       "25                   56.0                 245.0  0.99550  3.25       0.50   \n",
       "26                   35.0                 146.0  0.99300  3.45       0.44   \n",
       "27                   32.0                 141.0  0.99610  3.38       0.53   \n",
       "28                   17.0                 132.0  0.99140  3.19       0.49   \n",
       "29                   37.0                 114.0  0.99060  3.10       0.71   \n",
       "...                   ...                   ...      ...   ...        ...   \n",
       "4868                 42.0                 124.0  0.99324  3.31       0.64   \n",
       "4869                  8.0                  81.0  0.99626  3.19       0.51   \n",
       "4870                 29.0                 132.0  0.99188  3.15       0.36   \n",
       "4871                 20.0                  98.0  0.98970  3.37       0.55   \n",
       "4872                 50.0                 179.0  0.99622  3.14       0.60   \n",
       "4873                 33.0                 122.0  0.99044  3.33       0.52   \n",
       "4874                 16.0                 125.0  0.99282  3.49       0.49   \n",
       "4875                 18.0                  97.0  0.99245  3.12       0.41   \n",
       "4876                 34.0                 117.0  0.99132  3.36       0.59   \n",
       "4877                 12.0                  82.0  0.99286  3.25       0.36   \n",
       "4878                  6.0                  81.0  0.99234  3.24       0.35   \n",
       "4879                 68.0                 170.0  0.99494  3.15       0.50   \n",
       "4880                 68.0                 170.0  0.99494  3.15       0.50   \n",
       "4881                 34.0                 118.0  0.99540  3.07       0.50   \n",
       "4882                 45.0                 156.0  0.99184  3.26       0.38   \n",
       "4883                 60.0                 148.0  0.98964  3.27       0.35   \n",
       "4884                 68.0                 174.0  0.99492  3.14       0.50   \n",
       "4885                 68.0                 170.0  0.99494  3.15       0.50   \n",
       "4886                 45.0                 121.0  0.99168  3.21       1.08   \n",
       "4887                  5.0                  56.0  0.98928  3.04       0.79   \n",
       "4888                 38.0                 127.0  0.99330  3.04       0.54   \n",
       "4889                 34.0                 118.0  0.99540  3.07       0.50   \n",
       "4890                 25.0                 100.0  0.98938  3.06       0.44   \n",
       "4891                 38.0                 121.0  0.99074  3.24       0.46   \n",
       "4892                 29.0                 112.0  0.99298  3.29       0.54   \n",
       "4893                 24.0                  92.0  0.99114  3.27       0.50   \n",
       "4894                 57.0                 168.0  0.99490  3.15       0.46   \n",
       "4895                 30.0                 111.0  0.99254  2.99       0.46   \n",
       "4896                 20.0                 110.0  0.98869  3.34       0.38   \n",
       "4897                 22.0                  98.0  0.98941  3.26       0.32   \n",
       "\n",
       "        alcohol  quality  \n",
       "0      8.800000        6  \n",
       "1      9.500000        6  \n",
       "2     10.100000        6  \n",
       "3      9.900000        6  \n",
       "4      9.900000        6  \n",
       "5     10.100000        6  \n",
       "6      9.600000        6  \n",
       "7      8.800000        6  \n",
       "8      9.500000        6  \n",
       "9     11.000000        6  \n",
       "10    12.000000        5  \n",
       "11     9.700000        5  \n",
       "12    10.800000        5  \n",
       "13    12.400000        7  \n",
       "14     9.700000        5  \n",
       "15    11.400000        7  \n",
       "16     9.600000        6  \n",
       "17    12.800000        8  \n",
       "18    11.300000        6  \n",
       "19     9.500000        5  \n",
       "20    12.800000        8  \n",
       "21    11.000000        7  \n",
       "22    10.500000        8  \n",
       "23     9.300000        5  \n",
       "24    10.000000        6  \n",
       "25    10.400000        6  \n",
       "26    10.000000        6  \n",
       "27    10.500000        6  \n",
       "28    11.600000        6  \n",
       "29    12.300000        7  \n",
       "...         ...      ...  \n",
       "4868  10.800000        6  \n",
       "4869   9.800000        6  \n",
       "4870  11.450000        7  \n",
       "4871  12.050000        6  \n",
       "4872   9.700000        5  \n",
       "4873  11.900000        6  \n",
       "4874  10.000000        6  \n",
       "4875   9.700000        6  \n",
       "4876  11.600000        7  \n",
       "4877   8.800000        5  \n",
       "4878   9.500000        4  \n",
       "4879   9.533333        6  \n",
       "4880   9.533333        6  \n",
       "4881   9.400000        6  \n",
       "4882  10.700000        5  \n",
       "4883  11.500000        6  \n",
       "4884   9.600000        5  \n",
       "4885   9.550000        6  \n",
       "4886  12.150000        7  \n",
       "4887  13.000000        7  \n",
       "4888   9.200000        5  \n",
       "4889   9.400000        6  \n",
       "4890  11.800000        6  \n",
       "4891  10.600000        6  \n",
       "4892   9.700000        5  \n",
       "4893  11.200000        6  \n",
       "4894   9.600000        5  \n",
       "4895   9.400000        6  \n",
       "4896  12.800000        7  \n",
       "4897  11.800000        6  \n",
       "\n",
       "[4898 rows x 12 columns]>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.head"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def predict(row, coefficients):\n",
    "    yhat = coefficients[0]\n",
    "    for i in range(len(row)-1):\n",
    "        yhat += coefficients[i + 1] * row[i]\n",
    "    return yhat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Estimate linear regression coefficients using stochastic gradient descent\n",
    "def coefficients_sgd(train, l_rate, n_epoch):\n",
    "    coef = [0.0 for i in range(len(train[0]))]\n",
    "    print ('Coeficiente Inicial={0}' % (coef))\n",
    "    for epoch in range(n_epoch):\n",
    "        sum_error = 0\n",
    "        for row in train:\n",
    "            yhat = predict(row, coef)\n",
    "            error = yhat - row[-1]\n",
    "            sum_error += error**2\n",
    "            coef[0] = coef[0] - l_rate * error\n",
    "            for i in range(len(row)-1):\n",
    "                coef[i + 1] = coef[i + 1] - l_rate * error * row[i] \n",
    "        print(('epoch=%d, lrate=%.3f, error=%.3f' % (epoch, l_rate, sum_error)))\n",
    "    return coef"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "l_rate = 0.001\n",
    "n_epoch = 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "0",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m~/miniconda3/envs/data-science/lib/python3.6/site-packages/pandas/core/indexes/base.py\u001b[0m in \u001b[0;36mget_loc\u001b[0;34m(self, key, method, tolerance)\u001b[0m\n\u001b[1;32m   2441\u001b[0m             \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2442\u001b[0;31m                 \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   2443\u001b[0m             \u001b[0;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mpandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;32mpandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;31mKeyError\u001b[0m: 0",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-12-0dbea17cbc4e>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mcoef\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcoefficients_sgd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdataset\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ml_rate\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mn_epoch\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcoef\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m<ipython-input-4-0e02069505eb>\u001b[0m in \u001b[0;36mcoefficients_sgd\u001b[0;34m(train, l_rate, n_epoch)\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;31m# Estimate linear regression coefficients using stochastic gradient descent\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcoefficients_sgd\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtrain\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ml_rate\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mn_epoch\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m     \u001b[0mcoef\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m0.0\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtrain\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      4\u001b[0m     \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m'Coeficiente Inicial={0}'\u001b[0m \u001b[0;34m%\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mcoef\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m     \u001b[0;32mfor\u001b[0m \u001b[0mepoch\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mn_epoch\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/miniconda3/envs/data-science/lib/python3.6/site-packages/pandas/core/frame.py\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m   1962\u001b[0m             \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getitem_multilevel\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1963\u001b[0m         \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1964\u001b[0;31m             \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getitem_column\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1965\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1966\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m_getitem_column\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/miniconda3/envs/data-science/lib/python3.6/site-packages/pandas/core/frame.py\u001b[0m in \u001b[0;36m_getitem_column\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m   1969\u001b[0m         \u001b[0;31m# get column\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1970\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcolumns\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_unique\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1971\u001b[0;31m             \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_get_item_cache\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1972\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1973\u001b[0m         \u001b[0;31m# duplicate columns & possible reduce dimensionality\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/miniconda3/envs/data-science/lib/python3.6/site-packages/pandas/core/generic.py\u001b[0m in \u001b[0;36m_get_item_cache\u001b[0;34m(self, item)\u001b[0m\n\u001b[1;32m   1643\u001b[0m         \u001b[0mres\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcache\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mitem\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1644\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mres\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1645\u001b[0;31m             \u001b[0mvalues\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_data\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mitem\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1646\u001b[0m             \u001b[0mres\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_box_item_values\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mitem\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mvalues\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1647\u001b[0m             \u001b[0mcache\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mitem\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mres\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/miniconda3/envs/data-science/lib/python3.6/site-packages/pandas/core/internals.py\u001b[0m in \u001b[0;36mget\u001b[0;34m(self, item, fastpath)\u001b[0m\n\u001b[1;32m   3588\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   3589\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0misnull\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mitem\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 3590\u001b[0;31m                 \u001b[0mloc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mitem\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   3591\u001b[0m             \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   3592\u001b[0m                 \u001b[0mindexer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0marange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0misnull\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/miniconda3/envs/data-science/lib/python3.6/site-packages/pandas/core/indexes/base.py\u001b[0m in \u001b[0;36mget_loc\u001b[0;34m(self, key, method, tolerance)\u001b[0m\n\u001b[1;32m   2442\u001b[0m                 \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2443\u001b[0m             \u001b[0;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2444\u001b[0;31m                 \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_engine\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_loc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_maybe_cast_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   2445\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2446\u001b[0m         \u001b[0mindexer\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmethod\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtolerance\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtolerance\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32mpandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;32mpandas/_libs/index.pyx\u001b[0m in \u001b[0;36mpandas._libs.index.IndexEngine.get_loc\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;32mpandas/_libs/hashtable_class_helper.pxi\u001b[0m in \u001b[0;36mpandas._libs.hashtable.PyObjectHashTable.get_item\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;31mKeyError\u001b[0m: 0"
     ]
    }
   ],
   "source": [
    "coef = coefficients_sgd(dataset, l_rate, n_epoch)\n",
    "print(coef)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
